package freshmanspecial.mredrock.com.welcomefreshman.Model;


import freshmanspecial.mredrock.com.welcomefreshman.Bean.MilitaryTrainingPhotoBean;
import freshmanspecial.mredrock.com.welcomefreshman.Bean.MilitaryTrainingVideoBean;
import freshmanspecial.mredrock.com.welcomefreshman.Service.TrainService;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by Administrator on 2017/8/12 0012.
 */

public class TrainModel implements ITrainModel {

    private String api ="http://yangruixin.com/";

    @Override
    public void getTrainingVideo(final OnGetTrain onGetTrain) {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(api)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();
        TrainService service = retrofit.create(TrainService.class);
        service.getTrainingVideo("MilitaryTrainingVideo").
                subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<MilitaryTrainingVideoBean>() {
                    MilitaryTrainingVideoBean bean = null;
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(MilitaryTrainingVideoBean value) {
                        bean = value;
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete()
                    {
                        if(bean != null) onGetTrain.onFinish(bean);
                    }
                });
    }

    @Override
    public void getTrainingPhoto(final OnGetTrain onGetTrain) {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(api)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();
        TrainService service = retrofit.create(TrainService.class);
        service.getTrainingPhoto("MilitaryTrainingPhoto").
                subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<MilitaryTrainingPhotoBean>() {
                    MilitaryTrainingPhotoBean bean = null;
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(MilitaryTrainingPhotoBean value) {
                        bean = value;
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete()
                    {
                        if(bean != null) onGetTrain.onFinish(bean);
                    }
                });
    }

    public interface OnGetTrain{
        void onFinish(Object dataBean);
    }
}
